package com.aoxon.cargo.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ImageView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Picture;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.loader.NormalDataLoader;

public class ScrollViewPage implements
	NormalDataLoader.OnLoadDataListener{
	
	int childCount;
	protected static final int SCROLL = 1, AUTO_SCROLL_TIME = 5000,
			SETINDEX = 0;
	protected static boolean autoScroll = true;
	private List<Picture> imgs = new ArrayList<Picture>();
	private NormalDataLoader normalDataLoader ;
	private FileLoader myImageLoader = new FileLoader(300, 300, true);

	private Context context;
	private LayoutInflater inflate;
	private ViewPager vp;
	public ScrollViewPage(Context context , ViewPager vp) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.vp = vp;
	}
	
	public void setup(){
		inflate = LayoutInflater.from(context);
		normalDataLoader = new NormalDataLoader((Activity)context);
		normalDataLoader.setOnloadDataListener(this);
		normalDataLoader.load();
	}
	
	protected Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if (SCROLL == msg.what) {
				if (autoScroll) {
					vp.setCurrentItem(vp.getCurrentItem() + 1,
							true);
				}
				mHandler.sendEmptyMessageDelayed(SCROLL, AUTO_SCROLL_TIME);
			} else if (SETINDEX == msg.what) {
				vp.setCurrentItem(Integer.MAX_VALUE / 2
						- (Integer.MAX_VALUE / 2) % childCount);
			}

			// super.handleMessage(msg);
		}

	};
	
	class ViewPagerAdapter extends PagerAdapter {


	    public ViewPagerAdapter() {
	        super();
//	        this.inflate = LayoutInflater.from(activity);
//	        views.add(activity.getResources().getDrawable(R.drawable.banner1));
//	        views.add(activity.getResources().getDrawable(R.drawable.banner2));
//	        views.add(activity.getResources().getDrawable(R.drawable.banner3));
//	        ImageView iv = (ImageView) inflate.inflate(R.layout.banner_item, null);
//	        this.group = group;
	    }

	    @Override
	    public int getCount() {
	        return Integer.MAX_VALUE-Integer.MAX_VALUE%imgs.size();
	    }

	    // 来判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
	    @Override
	    public boolean isViewFromObject(View arg0, Object arg1) {
	        return arg0 == arg1;
	    }

	    // PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
	    @Override
	    public void destroyItem(ViewGroup view, int position, Object object) {
	        view.removeViewInLayout((ImageView) object);
	    }

	    // 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
	    @Override
	    public Object instantiateItem(ViewGroup view, int position) {
//	        Log.v("ViewPager", String.valueOf(group.getCurrentItem()%3));
//	        Log.v("BannerAdapter", String.valueOf(position));
//	        Log.v("BannerAdapter view position", String.valueOf(position % views.size()));
	        MyImageView iv = (MyImageView) inflate.inflate(R.layout.banner_item, null);
	        String path = imgs.get(position % imgs.size()).getPictureUrl();
	        myImageLoader.displayImage(path, iv, null, false);
//	        iv.setImageDrawable(views.get(position % views.size()));
	        view.addView(iv);
	        return iv;
	    }
	    
	}
	
	
	
	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		//加载banner
		//加载picture
		ViewPagerAdapter adapter = new ViewPagerAdapter();
		vp.setAdapter(adapter);
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		
	}
}


class MyImageView extends ImageView {

    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, width/2);
    }
}